<template>
  <div class="update-birthday">
    <van-datetime-picker
      v-model="currentDate"
      type="date"
      title="选择年月日"
      :min-date="minDate"
      :max-date="maxDate"
      @cancel="$emit('close')"
      @confirm="onConfirm"
    />
  </div>
</template>

<script>
import { updateUserProfile } from "@/api/user";
import dayjs from "dayjs";
export default {
  // 组件名称
  name: "UpdateBirthday",
  // 组件参数 接收来自父组件的数据
  props: {
    value: {
      type: String,
      required: true,
    },
  },
  // 局部注册的组件
  components: {},
  // 组件状态值
  data() {
    return {
      minDate: new Date(1934, 3, 4),
      maxDate: new Date(),
      currentDate: new Date(this.value), //默认选中
    };
  },
  // 计算属性
  computed: {},
  // 侦听器
  watch: {},
  created() {},
  mounted() {},
  // 组件方法
  methods: {
    async onConfirm() {
      //请求提交
      //更新父组件
      //关闭弹窗
      this.$toast.loading({
        message: "保存中...",
        forbidClick: true,
      });
      try {
        /* const date = `${this.currentDate.getFullYear()}-${this.currentDate.getMonth()}-${this.currentDate.getDate()}`; */
        const date = dayjs(this.currentDate).format("YYYY-MM-DD");
        await updateUserProfile({
          birthday: date,
        });
        this.$emit("input", date);
        this.$emit("close");
        this.$toast.success("保存成功");
      } catch (err) {}
    },
  },
};
</script> 

<style scoped lang='less'>
</style>
